/**
  校际交流审核-学院
*/
<template>
    <div>
        <div v-if="ifShowList === 'list'">
            <condition-card
                @reset="reset"
                @search="onSubmit"
                label-position="right"
                label-width="150px"
            >
                <el-row :gutter="24" style="height: 50px;">
                    <el-col :span="8">
                        <el-form-item label="国家/地区">
                            <el-select
                                v-model.trim="queryForm.condition.projectNationCode"
                                clearable
                                filterable
                                style="width: 100%"
                                placeholder
                            >
                                <el-option
                                    v-for="item in options.country"
                                    :key="item.value"
                                    :label="item.label"
                                    :value="item.value"
                                ></el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="审核状态">
                            <el-select
                                v-model.trim="queryForm.condition.applyStatus"
                                clearable
                                style="width: 100%"
                                filterable
                                placeholder
                            >
                                <el-option
                                    v-for="item in options.states"
                                    :key="item.value"
                                    :label="item.label"
                                    :value="item.value"
                                ></el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="项目开始时间">
                            <el-date-picker
                                v-model="queryForm.condition.projectStartTimeStr"
                                type="date"
                                style="width:100%"
                                :disabled="ifShowDetail"
                                value-format="yyyy-MM-dd"
                                :picker-options="startDatePicker"
                            ></el-date-picker>
                        </el-form-item>
                    </el-col>
                </el-row>

                <el-row :gutter="24" style="height: 50px;">
                    <el-col :span="8">
                        <el-form-item label="项目结束时间">
                            <el-date-picker
                                v-model="queryForm.condition.projectEndTimeStr"
                                type="date"
                                style="width:100%"
                                :disabled="ifShowDetail"
                                value-format="yyyy-MM-dd"
                                :picker-options="endDatePicker"
                            ></el-date-picker>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="培养层次">
                            <el-select
                                v-model.trim="queryForm.condition.trainingCodes"
                                clearable
                                filterable
                                multiple
                                style="width: 100%"
                                placeholder
                            >
                                <el-option
                                    v-for="item in options.teachLevel"
                                    :key="item.value"
                                    :label="item.label"
                                    :value="item.value"
                                ></el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="留学内容">
                            <el-input
                                v-model="queryForm.condition.projectStudyAbroadContent"
                                class="topic-input-select"
                                maxlength="32"
                            ></el-input>
                        </el-form-item>
                    </el-col>
                </el-row>

                <el-row :gutter="24" style="height: 50px;margin-left: -13px;">
                    <el-col :span="8">
                        <el-form-item label="工号">
                            <el-input
                                style="width: 100%"
                                v-model.trim="queryForm.condition.teacherId"
                            ></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="姓名">
                            <el-input
                                style="width: 100%"
                                v-model.trim="queryForm.condition.teacherName"
                            ></el-input>
                        </el-form-item>
                    </el-col>
                </el-row>
                <!-- 搜索框结束 -->

                <!-- 列表页开始 -->
            </condition-card>
            <el-card shadow="never" v-loading="loading" body-style="padding: 24px;">
                <h1 class="condition-title">校际交流院级项目审核列表</h1>
                <div class="sub-title">
                    <div class="float-right" style="display: flex;">
                        <!-- 导出 -->
                        <el-button @click="exportMenu()">导出</el-button>
                        <!-- 驳回 -->
                        <el-button type="danger" @click="teacherAudit(2)">驳回</el-button>
                        <!-- 通过 -->
                        <el-button type="primary" @click="teacherAudit(1)">通过</el-button>
                    </div>
                </div>
                <edu-table
                    :pagination="false"
                    :order="true"
                    :tooltipEffect="tooltipEffect"
                    stripe
                    :data="dataList"
                    size="mini"
                    @selection-change="handleSelectionChange"
                >
                    <edu-table-column label="工号" prop="teacherId"></edu-table-column>
                    <edu-table-column label="姓名" prop="teacherName"></edu-table-column>
                    <edu-table-column label="培养层次" prop="trainingName"></edu-table-column>
                    <edu-table-column label="项目名称" prop="projectName"></edu-table-column>
                    <edu-table-column label="国家/地区" prop="projectNationName"></edu-table-column>
                    <edu-table-column label="合作大学" prop="projectCollegeCn"></edu-table-column>
                    <edu-table-column label="项目开始时间" prop="projectStartTime" width="120"></edu-table-column>
                    <edu-table-column label="项目结束时间" prop="projectEndTime" width="120"></edu-table-column>
                    <edu-table-column label="每年的中方留学生名额" prop="projectInlandNumber" width="160"></edu-table-column>
                    <edu-table-column label="每年的外方留学生名额" prop="projectOutlandNumber" width="160"></edu-table-column>
                    <edu-table-column label="项目所涉及学院" prop="facultyName" width="120"></edu-table-column>
                    <edu-table-column label="留学内容" prop="projectStudyAbroadContent"></edu-table-column>
                    <edu-table-column label="审核状态" prop="applyStatus">
                        <template slot-scope="scope">
                            <el-tag
                                v-if="scope.row.applyStatus !==null"
                                :type="scope.row.applyStatus | getType"
                            >{{scope.row.applyStatus | applyStatus}}</el-tag>
                        </template>
                    </edu-table-column>
                    <edu-table-column label="下一级审批人" prop="approverName" width="120"></edu-table-column>
                    <edu-table-column label="启用状态" prop="projectEnable">
                        <template slot-scope="scope">
                            <el-tag
                                v-if="scope.row.projectEnable !==null"
                                :type="scope.row.projectEnable | getEnable"
                            >{{scope.row.projectEnable | projectEnable}}</el-tag>
                        </template>
                    </edu-table-column>
                    <!-- 操作 -->
                    <edu-table-column :label="$t('base.operate')" fit width="200">
                        <template slot-scope="scope">
                            <el-button type="text" @click="viewAudit(scope.row)">查看</el-button>
                            <el-button
                                type="text"
                                v-show="scope.row.projectEnable === 1 && scope.row.applyStatus === 4 && scope.row.canEnable"
                                @click="changeSataus(scope.row, 0)"
                            >停用</el-button>
                            <el-button
                                type="text"
                                v-show="scope.row.projectEnable === 0 && scope.row.applyStatus === 4 && scope.row.canEnable"
                                @click="changeSataus(scope.row, 1)"
                            >启用</el-button>
                            <el-button
                                type="text"
                                v-show="(scope.row.applyStatus === 2 || scope.row.applyStatus === 3) && scope.row.canAudit"
                                @click="schoolAudit(scope.row)"
                            >审核</el-button>
                        </template>
                    </edu-table-column>
                </edu-table>
                <el-row type="flex" class="row-bg" justify="end">
                    <el-col>
                        <el-pagination
                            @size-change="handleSizeChange"
                            @current-change="handleCurrentChange"
                            :current-page="queryForm.pageNum_"
                            :page-sizes="[20, 40, 80, 100]"
                            :page-size="queryForm.pageSize_"
                            layout="total, sizes, prev, pager, next, jumper"
                            :total="queryForm.total_"
                            style="float:right;margin-top:10px"
                        ></el-pagination>
                    </el-col>
                </el-row>
            </el-card>
        </div>
        <div v-if="ifShowList ==='view'">
            <inter-School-Com-View
                @returnVal="changekey"
                :projectId="projectId"
                :pageFrom="pageFrom"
            ></inter-School-Com-View>
        </div>
        <div v-if="ifShowList ==='audit'">
            <inter-School-Com-Audit @returnVal="changekey" :projectId="projectId"></inter-School-Com-Audit>
        </div>
        <el-dialog
            title="审核意见"
            :visible.sync="dialogFormVisible"
            :close-on-click-modal="false"
            :close-on-press-escape="false"
        >
            <el-form ref="contentForm" :model="contentForm">
                <el-form-item label="审批说明" prop="content">
                    <el-input
                        type="textarea"
                        :rows="4"
                        placeholder="请输入内容"
                        maxlength="500"
                        v-model="contentForm.content"
                    ></el-input>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="closeContent">取 消</el-button>
                <el-button type="primary" @click="auditSchool">确 定</el-button>
            </div>
        </el-dialog>
        <edu-export-dialog :title="$t('message.prompt')" ref="downloadRef"></edu-export-dialog>
    </div>
</template>
<script>
import InterSchoolComAppApi from 'common/src/api/interSchoolExchange/InterSchoolComApp'
import { queryDic } from 'common/src/api/dictionary'
import UTILS from 'common/src/utils/utils'
import InterSchoolComAudit from './common/InterSchoolComAudit'
import InterSchoolComView from './common/InterSchoolComView'
export default {
    components: {
        'inter-School-Com-View': InterSchoolComView,
        'inter-School-Com-Audit': InterSchoolComAudit
    },
    data() {
        return {
            queryForm: {
                pageNum_: 1,
                pageSize_: 20,
                total_: 0,
                condition: {
                    entrance: 2,
                    projectNationCode: '',
                    applyStatus: '',
                    projectStartTimeStr: '',
                    projectEndTimeStr: '',
                    projectStudyAbroadContent: '',
                    teacherId: '',
                    teacherName: '',
                    trainingCodes: []
                }
            },
            pageFrom: '',
            projectId: null,
            startDatePicker: this.beginDate(),
            endDatePicker: this.processDate(),
            auditType: null,
            dataList: [],
            contentForm: {
                content: ''
            },
            ifShowList: 'list',
            dialogFormVisible: false,
            multipleSelection: {
                ids: [],
                status: []
            },
            options: {
                country: [],
                teachLevel: [],
                states: [
                    {
                        label: '已提交',
                        value: 2
                    },
                    {
                        label: '待审核',
                        value: 3
                    },
                    {
                        label: '已通过',
                        value: 4
                    }
                ]
            }
        }
    },

    created() {
        // 初始化列表页
        this.getListData()
        this.getCountry()
        this.getTeacherLevel()
    },
    filters: {
        applyStatus(val) {
            switch (val) {
                case 1:
                    return '草稿'
                case 2:
                    return '已提交'
                case 3:
                    return '待审核'
                case 4:
                    return '已通过'
                case 5:
                    return '已驳回'
                default:
                    return ' '
            }
        },
        getType(val) {
            switch (val) {
                case 1:
                    return 'info'
                case 2:
                    return 'info'
                case 3:
                    return 'info'
                case 4:
                    return 'success'
                case 5:
                    return 'warning'
                default:
                    return val
            }
        },
        projectEnable(val) {
            switch (val) {
                case 1:
                    return '启用'
                case 0:
                    return '停用'
                default:
                    return ' '
            }
        },
        getEnable(val) {
            switch (val) {
                case 1:
                    return 'success'
                case 0:
                    return 'danger'
                default:
                    return ''
            }
        }
    },
    methods: {
        // 字典获取 国家
        async getCountry() {
            // 筛选条件字典查询
            let data = {
                lang: this.$store.getters.language || 'cn',
                type: 'allChild',
                keys: [
                    'G_GJQU' // 国家/地区
                ]
            }
            await queryDic(data).then(res => {
                UTILS.fillSelect(
                    {
                        country: 'G_GJQU' // 国家/地区
                    },
                    res.data,
                    this.options
                )
            })
        },

        // 获取培养层次
        getTeacherLevel() {
            InterSchoolComAppApi.queryTrainings().then(res => {
                res.data.forEach(level => {
                    this.options.teachLevel.push({
                        label: level.trainingName,
                        value: level.trainingCode
                    })
                })
            })
        },
        beginDate() {
            let _this = this
            return {
                disabledDate(time) {
                    if (_this.queryForm.condition.projectEndTimeStr) {
                        //如果结束时间不为空，则小于结束时间
                        return (
                            new Date(_this.queryForm.condition.projectEndTimeStr).getTime() <
                            time.getTime()
                        )
                    } else {
                        // return time.getTime() > Date.now()//开始时间不选时，结束时间最大值小于等于当天
                    }
                }
            }
        },
        processDate() {
            let _this = this
            return {
                disabledDate(time) {
                    if (_this.queryForm.condition.projectStartTimeStr) {
                        //如果开始时间不为空，则结束时间大于开始时间
                        return (
                            new Date(_this.queryForm.condition.projectStartTimeStr).getTime() >
                            time.getTime() + 24 * 60 * 60 * 1000
                        )
                    } else {
                        // return time.getTime() > Date.now()//开始时间不选时，结束时间最大值小于等于当天
                    }
                }
            }
        },

        // 导出目录
        exportMenu() {
            let _this = this
            let parm = {
                entrance: 2,
                projectNationCode: this.queryForm.condition.projectNationCode,
                applyStatus: this.queryForm.condition.applyStatus,
                projectStartTimeStr: this.queryForm.condition.projectStartTimeStr,
                projectEndTimeStr: this.queryForm.condition.projectEndTimeStr,
                projectStudyAbroadContent: this.queryForm.condition.projectStudyAbroadContent,
                teacherId: this.queryForm.condition.teacherId,
                teacherName: this.queryForm.condition.teacherName,
                trainingCodes: this.queryForm.condition.trainingCodes,
                selectedProjectIdList: this.multipleSelection.ids
            }
            if (this.dataList.length === 0) {
                this.$message.error('无数据')
            } else {
                this.$refs.downloadRef.open()
                InterSchoolComAppApi.exportProject(parm).then(res => {
                    if (res.data && res.data.key) {
                        this.exportNext(res.data.key)
                    }
                })
            }
        },
        exportNext(key) {
            setTimeout(() => {
                InterSchoolComAppApi.exportExcelS(key).then(resS => {
                    if (resS.data && resS.data.path) {
                        InterSchoolComAppApi.exportExcelT(resS.data.path).then(resT => {
                            this.$message({
                                message: this.$t('base.exportSuccess'),
                                type: 'success'
                            })
                            this.$refs.downloadRef.close()
                        })
                    } else {
                        this.exportNext(key)
                    }
                })
            }, 3000)
        },
        // 申请页返回
        changekey(val) {
            this.ifShowList = val
            this.getListData()
        },
        // 关闭弹框
        closeContent() {
            this.dialogFormVisible = false
        },
        teacherAudit(type) {
            let flag = true
            if (this.multipleSelection.ids.length === 0) {
                this.$message.error('请勾选数据')
            } else {
                this.multipleSelection.status.forEach(val => {
                    if (val.canAudit !== true) {
                        flag = false
                    }
                })

                if (flag) {
                    this.contentForm.content = ''
                    this.auditType = type
                    this.dialogFormVisible = true
                } else {
                    this.$message.error('当前勾选存在不可审核数据')
                }
            }
        },
        // 批量审核
        auditSchool() {
            let parm = {
                type: this.auditType,
                content: this.contentForm.content,
                projectIds: this.multipleSelection.ids
            }
            if (this.multipleSelection.ids.length > 0) {
                if (this.auditType === 2 && this.contentForm.content === '') {
                    this.$message.error('审批说明不可为空')
                } else {
                    InterSchoolComAppApi.approval(parm).then(res => {
                        if (res.code === 200) {
                            this.$message({
                                message: '审核成功',
                                type: 'success'
                            })
                            this.dialogFormVisible = false
                            this.getListData()
                        }
                    })
                }
            }
        },

        // 启停用
        changeSataus(row, type) {
            let parm = {
                projectId: row.projectId,
                projectEnable: type
            }
            InterSchoolComAppApi.enable(parm).then(res => {
                if (res.code === 200) {
                    if (type === 1) {
                        this.$message({
                            message: '启用成功',
                            type: 'success'
                        })
                    } else if (type === 0) {
                        this.$message({
                            message: '停止成功',
                            type: 'success'
                        })
                    }
                }
                this.getListData()
            })
        },

        // 查看
        viewAudit(row) {
            this.ifShowList = 'view'
            this.projectId = row.projectId
            this.pageFrom = 'audit'
        },
        // 审核
        schoolAudit(row) {
            this.ifShowList = 'audit'
            this.projectId = row.projectId
            this.pageFrom = 'audit'
        },

        // 重置筛选框
        reset() {
            this.queryForm = {
                pageNum_: 1,
                pageSize_: 20,
                total_: 0,
                condition: {
                    entrance: 2,
                    projectNationCode: '',
                    applyStatus: '',
                    projectStartTimeStr: '',
                    projectEndTimeStr: '',
                    projectStudyAbroadContent: '',
                    teacherId: '',
                    teacherName: '',
                    trainingCodes: []
                }
            }
            this.getListData()
        },

        // 查询按钮
        onSubmit() {
            this.queryForm.pageNum_ = 1
            this.queryForm.total_ = 0
            this.queryForm.pageSize_ = 20
            this.getListData()
        },

        // 获取列表
        getListData() {
            let _this = this
            this.dataList = []
            this.multipleSelection.ids = []
            this.multipleSelection.status = []
            InterSchoolComAppApi.list(this.queryForm).then(res => {
                res.data.list.forEach(revList => {
                    revList.trainings.forEach(tra => {
                        if (revList.trainingName) {
                            revList.trainingName = revList.trainingName + ',' + tra.trainingName
                        } else {
                            revList.trainingName = tra.trainingName
                        }
                    })
                    revList.faculties.forEach(fca => {
                        if (revList.facultyName) {
                            revList.facultyName = revList.facultyName + ',' + fca.facultyName
                        } else {
                            revList.facultyName = fca.facultyName
                        }
                    })
                    _this.dataList.push(revList)
                })

                _this.queryForm.pageSize_ = res.data.pageSize_
                _this.queryForm.total_ = res.data.total_
                _this.queryForm.pageNum_ = res.data.pageNum_
            })
        },
        handleSelectionChange(val) {
            this.multipleSelection.ids = []
            this.multipleSelection.status = []
            val.forEach(menu => {
                this.multipleSelection.status.push({
                    applyStatus: menu.applyStatus,
                    canAudit: menu.canAudit
                })
                this.multipleSelection.ids.push(menu.projectId)
            })
        },
        handleSizeChange(val) {
            this.queryForm.pageSize_ = val
            this.getListData()
        },
        handleCurrentChange(val) {
            this.queryForm.pageNum_ = val
            this.getListData()
        }
    }
}
</script>
<style lang="scss" scoped>
.condition-title {
    color: #2b3b4e;
    font-size: 18px;
    line-height: 24px;
    font-weight: bold;
}
.sub-title {
    margin: 16px 0 65px 0;
    font-size: 16px;
    color: #000020;
    letter-spacing: 0;
}
</style>
